package com.hoshiicloud.market.controller.commission;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.market.entity.commission.CommissionRecords;
import com.hoshiicloud.market.param.commission.CommissionRecordsAddParam;
import com.hoshiicloud.market.param.commission.CommissionRecordsQueryParam;
import com.hoshiicloud.market.param.commission.CommissionRecordsUpdateParam;
import com.hoshiicloud.market.service.commission.CommissionRecordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author:campchen
 * @Date:Created in 下午 02:11 2019/7/27
 * @PACKAGENAME com.hoshiicloud.market.controller.commission
 * @Description
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/market/commission/commission-records")
@Api(value = "分佣流水", tags = {"分佣流水"})
public class CommissionRecordsController {
    @Autowired
    private CommissionRecordsService commissionRecordsService;

    @PostMapping("/getPage")
    @ApiOperation(value = "分页查询佣金配置")
    private Response page(@RequestBody @Valid CommissionRecordsQueryParam commissionRecordsQueryParam){
        try{
        QueryWrapper<CommissionRecords> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(commissionRecordsQueryParam.getCompanyid()!=null,CommissionRecords::getCompanyid,commissionRecordsQueryParam.getCompanyid())
        .eq(commissionRecordsQueryParam.getPlatformid()!=null,CommissionRecords::getPlatformid,commissionRecordsQueryParam.getPlatformid())
        .eq(commissionRecordsQueryParam.getStoreid()!=null,CommissionRecords::getStoreid,commissionRecordsQueryParam.getStoreid())
        .eq(commissionRecordsQueryParam.getTxtype()!=null,CommissionRecords::getTxtype,commissionRecordsQueryParam.getTxtype())
        .eq(commissionRecordsQueryParam.getOrderId()!=null,CommissionRecords::getOrderId,commissionRecordsQueryParam.getOrderId())
        .eq(StringUtils.isNotBlank(commissionRecordsQueryParam.getDescription()),CommissionRecords::getDescription,commissionRecordsQueryParam.getDescription())
        .eq(commissionRecordsQueryParam.getAccountType()!=null,CommissionRecords::getAccountType,commissionRecordsQueryParam.getAccountType())
        .eq(commissionRecordsQueryParam.getAccountId()!=null,CommissionRecords::getAccountId,commissionRecordsQueryParam.getAccountId())
        .eq(commissionRecordsQueryParam.getDc()!=null,CommissionRecords::getDc,commissionRecordsQueryParam.getDc())
        .eq(commissionRecordsQueryParam.getAmount()!=null,CommissionRecords::getAmount,commissionRecordsQueryParam.getAmount())
        .eq(commissionRecordsQueryParam.getBanance()!=null,CommissionRecords::getBanance,commissionRecordsQueryParam.getBanance())
        .eq(commissionRecordsQueryParam.getDeleted()!=null,CommissionRecords::getDeleted,commissionRecordsQueryParam.getDeleted())
        .eq(commissionRecordsQueryParam.getStatus()!=null,CommissionRecords::getStatus,commissionRecordsQueryParam.getStatus())
        .eq(commissionRecordsQueryParam.getRefid()!=null,CommissionRecords::getRefid,commissionRecordsQueryParam.getRefid());
        String key = commissionRecordsQueryParam.toString() + ",Page{pageNumber=" + commissionRecordsQueryParam.getPageNumber() + ",pageSize=" + commissionRecordsQueryParam.getPageSize() + "}";
        IPage iPage=new Page(Long.parseLong(commissionRecordsQueryParam.getPageNumber()),Long.parseLong(commissionRecordsQueryParam.getPageSize()));
        //TODO 如果数据库无数据 会返回成功参数为 false 返回码为-100的数据回去。此处应该用参数接收，进行判断。
        return Response.data(commissionRecordsService.getPage(iPage,queryWrapper,key));
        }catch (Exception e){
            log.error(e.getMessage());
            return Response.failed(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询佣金配置")
    public Response getCommissionRecordsById(@PathVariable Long id){
        return Response.data(commissionRecordsService.getCommissionRecordsById(id));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增佣金配置")
    public Response save(@RequestBody @Valid CommissionRecordsAddParam commissionRecordsAddParam) {
        CommissionRecords commissionRecords;
        try {
            commissionRecords = BeanUtils.covertType(commissionRecordsAddParam, CommissionRecords.class);
        } catch (Exception e) {
            log.error("新增佣金流水Bean转换异常", e);
            return Response.failed("新增流水Bean转换异常");
        }
        return Response.data(commissionRecordsService.insert(commissionRecords));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改佣金配置")
    public Response update(@RequestBody @Valid CommissionRecordsUpdateParam commissionRecordsUpdateParam) {
        CommissionRecords commissionRecords;
        try {
            commissionRecords = BeanUtils.covertType(commissionRecordsUpdateParam, CommissionRecords.class);
        } catch (Exception e) {
            log.error("修改佣金流水Bean转换异常", e);
            return Response.failed("新增佣金流水Bean转换异常");
        }
        return Response.data(commissionRecordsService.updateById(commissionRecords));
    }
}
